﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Text;
using System.Security.Cryptography;
using System.IO;

using CE.Security;
using CE.User;
using CE.Global;
using CE.Configuration;

public partial class MasterPage : System.Web.UI.MasterPage
{
    string selectedLanguage;
    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlLink css = new HtmlLink();
        css.Href = Resources.CarMenu.CssUrl;
        css.Attributes["rel"] = "stylesheet";
        css.Attributes["type"] = "text/css";
        css.Attributes["media"] = "all";
        Page.Header.Controls.Add(css);

        string _strIsMaintenanceActive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.IsMaintenanceActive");
        if (_strIsMaintenanceActive == "1")
        {
            Response.Redirect("~/maintenance/index.html");
        }
        string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("Mobile.IsCXLive");
        if (_strCXLive == "0")
        {
            if (Session[CE.Global.General.Guest] == null)
            {
                Response.Redirect("~/Default.aspx");
            }
        }
        if (!IsPostBack)
        {
            if (Session["Language"] != null)
            {
                if (Session["Language"].ToString() == "th-TH")
                {
                    ibtnLanguage.ImageUrl = "images/icons/flag-en.png";
                    ibtnLanguageSticky.ImageUrl = "images/icons/flag-en.png";
                }
                else if (Session["Language"].ToString() == "en-US")
                {
                    ibtnLanguage.ImageUrl = "images/icons/flag-th.png";
                    ibtnLanguageSticky.ImageUrl = "images/icons/flag-th.png";
                }
            }
            HttpCookie cookies = Request.Cookies["GGMobileQuote"];
            if (cookies != null)
            {
                if (Session[CE.Global.General.UserId] == null)
                {
                    string Email = MySecurity.Decrypt(cookies.Values.Get("CustomerEmail"), true);
                    GetUserDetails(Email);
                }
            }
        }
    }

    protected void ibtnLanguage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        selectedLanguage = string.Empty;

        if (Session["Language"] != null)
        {
            selectedLanguage = Session["Language"].ToString();
        }
        else
        {
            selectedLanguage = "th-TH";
            Session["Language"] = selectedLanguage;
        }

        if (selectedLanguage == "th-TH")
        {
            Session["Language"] = "en-US";
        }
        else
        {
            Session["Language"] = "th-TH";
        }

        lblLanguage.Text = selectedLanguage;

        if (selectedLanguage.ToString() == "th-TH")
        {
            ibtnLanguage.ImageUrl = "images/icons/flag-en.png";
            ibtnLanguageSticky.ImageUrl = "images/icons/flag-en.png";
        }
        else if (selectedLanguage.ToString() == "en-US")
        {
            ibtnLanguage.ImageUrl = "images/icons/flag-en.png";
            ibtnLanguageSticky.ImageUrl = "images/icons/flag-en.png";
        }

        Response.Redirect(Request.UrlReferrer.AbsoluteUri);

    }

    private void GetUserDetails(string CustomerEmail)
    {
        try
        {
            AccountDB _accountdb = new AccountDB();
            Account _account = _accountdb.GetUserAccount(CustomerEmail.ToLower(), 400);
            if (_account != null)
            {
                Session[CE.Global.General.UserId] = _account.CEID;
                Session[CE.Global.General.UserTypeId] = _account.UserTypeId;
                Session[CE.Global.General.UserEmail] = _account.Email;

                Profile _profile = new Profile();
                ProfileDB _profiledb = new ProfileDB();
                Contact _contact = new Contact();
                ContactDB _contactdb = new ContactDB();

                _contact = _contactdb.GetContact(_account.CEID);
                _profile = _profiledb.GetProfile(_account.CEID);

                if (_contact != null)
                {
                    Session[CE.Global.General.UserPhoneNo] = _contact.Mobile;
                }
                else
                {
                    Session[CE.Global.General.UserPhoneNo] = null;
                }
                if (_profile != null)
                {
                    if (_profile.FirstName != null && _profile.FirstName != string.Empty)
                    {
                        Session[CE.Global.General.UserName] = Server.HtmlDecode(_profile.FirstName) + " " + Server.HtmlDecode(_profile.SurName);
                    }
                    else
                    {
                        Session[CE.Global.General.UserName] = Server.HtmlDecode(_profile.SurName);
                    }
                }
                if (_profile.IsAcceptTC != null)
                {
                    Session[CE.Global.General.IsAcceptTC] = "0";
                    if (_profile.IsAcceptTC == true)
                    {
                        Session[CE.Global.General.IsAcceptTC] = "1";
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

}
